package com.atguigu.gulimall.product.service.impl;

import com.atguigu.gulimall.product.dao.BrandDao;
import com.atguigu.gulimall.product.dao.CategoryDao;
import com.atguigu.gulimall.product.entity.BrandEntity;
import com.atguigu.gulimall.product.entity.CategoryEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.product.dao.CategoryBrandRelationDao;
import com.atguigu.gulimall.product.entity.CategoryBrandRelationEntity;
import com.atguigu.gulimall.product.service.CategoryBrandRelationService;
import org.springframework.transaction.annotation.Transactional;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {

    @Autowired
    private CategoryDao categoryDao;

    @Autowired
    private BrandDao brandDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 方法说明：
     *
     * @MethodName 新增品牌与商品分类的关联信息
     * @Description    
     * @Author chenCheng
     * @EMAIL <2277411582@qq.com>
     * @Date 2022/9/24 15:53  
     * @修改人及日期：
     * @修改说明：
     */
    @Override
    public void saveDetail(CategoryBrandRelationEntity categoryBrandRelation) {
        // 得到品牌的id与商品分类的id
        Long catelogId = categoryBrandRelation.getCatelogId();
        Long brandId = categoryBrandRelation.getBrandId();

        // 查询出当前品牌的名称
        BrandEntity brandEntity = brandDao.selectById(brandId);
        // 查询出当前商品分类的名称
        CategoryEntity categoryEntity = categoryDao.selectById(catelogId);

        categoryBrandRelation.setBrandName(brandEntity.getName());
        categoryBrandRelation.setCatelogName(categoryEntity.getName());

        this.save(categoryBrandRelation);
    }

    /**
     * 方法说明：
     *
     * @MethodName 更新品牌的信息
     * @Description    
     * @Author chenCheng
     * @EMAIL <2277411582@qq.com>
     * @Date 2022/9/24 16:20  
     * @修改人及日期：
     * @修改说明：
     */
    @Override
    public void updateBrand(Long brandId, String name) {
        // 先查询在更新
        int count = this.count(new QueryWrapper<CategoryBrandRelationEntity>().eq("brand_id",brandId));
        if(count > 0){
            CategoryBrandRelationEntity relationEntity = new CategoryBrandRelationEntity();
            relationEntity.setBrandId(brandId);
            relationEntity.setBrandName(name);
            this.update(relationEntity,new QueryWrapper<CategoryBrandRelationEntity>().eq("brand_id",brandId));
        }
    }

    /**
     * 方法说明：
     *
     * @MethodName 更新商品分类的信息
     * @Description    
     * @Author chenCheng
     * @EMAIL <2277411582@qq.com>
     * @Date 2022/9/24 16:40  
     * @修改人及日期：
     * @修改说明：
     */
    @Transactional
    @Override
    public void updateCategory(Long catId, String name) {
        // 判断当前的这个catId是不是3级分类的id,我们在这个中间表中存的分类的名称其实只是3级分类的名称
        // 先去这张中间表查有没有这个分类的信息
        int  count = this.count(new QueryWrapper<CategoryBrandRelationEntity>().eq("catelog_id", catId));
        if(count > 0){
            this.baseMapper.updateCategory(catId,name);
        }

    }

    /**
     * 方法说明：
     *
     * @MethodName 获取当前分类关联的品牌信息
     * @Description
     * @Author chenCheng
     * @EMAIL <2277411582@qq.com>
     * @Date 2022/9/27 14:27
     * @修改人及日期：
     * @修改说明：
     */
    @Override
    public List<BrandEntity> getBrandsByCatId(Long catId) {

        // 1.去pms_category_brand_relation中间表中查询出品牌的id
        List<CategoryBrandRelationEntity> entities = this.list(new QueryWrapper<CategoryBrandRelationEntity>().eq("catelog_id", catId));

        // 2.收集品牌的id
        List<Long> ids = entities.stream().map(item -> {
            return item.getBrandId();
        }).collect(Collectors.toList());

        // 收集品牌的信息
        List<BrandEntity> brandEntities = brandDao.selectBatchIds(ids);
        return brandEntities;
    }

}